"ui";
activity.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.window.setStatusBarColor(android.graphics.Color.TRANSPARENT);
activity.window.getDecorView().setSystemUiVisibility(android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE | android.view.View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
var oneAppRunTime;

ui.layout(
    <vertical>
        <linear id="appbar" orientation="vertical" h="75">
            <View h="25" w="*"/>
            <frame h="50">
                <img id="back" src="@drawable/ic_arrow_back_black_48dp" padding="12" tint="#ffffff" w="50" h="50" foreground="?selectableItemBackground"/>
                <text id="app_name" textSize="18sp" textStyle="bold" textColor="#ffffff" w="*" text="游戏助手" h="50" gravity="center"/>
            </frame>
        </linear>
        <frame h="*">
            <vertical>
                <vertical>

                    
                </vertical>
                <vertical id="start_params" visibility="gone">
                    <View w="*" h="10" bg="#f4f5f6" />
                    <View w="*" h="1" bg="#E0E0E0" />

                    <horizontal h="40" margin="10 0 10 0">
                        <text textSize="14sp" textColor="black" text="App启动等待时间" w="0" h="40" gravity="center_vertical" layout_weight="1"/>
                        <input id="startWaitTime" textSize="14sp" textColor="black" inputType="number" w="120" h="40"  text="8" hint="8" padding="10 0 0 0" />
                        <text textSize="14sp"  text="秒" textColor="black"  w="auto" h="40"/>
                    </horizontal>

                    <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E0E0E0'></horizontal>

                    <horizontal h="40" margin="10 0 10 0">
                        <text textSize="14sp" textColor="black" text="单个App运行时间" w="auto" h="40" gravity="center_vertical" layout_weight="1"/>
                        <input id="oneAppRunTime" textSize="14sp" textColor="black" inputType="number" w="120" h="40"  text="40" hint="0" padding="10 0 0 0" />
                        <text textSize="14sp"  text="分" textColor="black"  w="auto" h="40"/>
                    </horizontal>

                    <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E0E0E0'></horizontal>

                    <horizontal h="40" margin="10 0 10 0">
                        <text textSize="14sp" textColor="black" text="程序每日执行次数" w="auto" h="40" gravity="center_vertical" layout_weight="1"/>
                        <input id="everydayNumber" textSize="14sp" textColor="black" inputType="number" w="120" h="40"  text="4" hint="0" padding="10 0 0 0" />
                        <text textSize="14sp"  text="轮" textColor="black"  w="auto" h="40"/>
                    </horizontal>

                    <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E0E0E0'></horizontal>
                    <horizontal h="40" margin="10 0 10 0">
                        <Switch textSize="14sp" textColor="black" id="random_up"  text="视频随机点赞(10%)" checked="true" layout_weight="1" h="40"/>
                    </horizontal>
                    
                    <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E0E0E0'></horizontal>
                    <horizontal h="40" margin="10 0 10 0">
                        <Switch textSize="14sp" textColor="black" id="auto_refresh"  text="随机时间重新获取视频" checked="true" layout_weight="1" h="40"/>
                    </horizontal>
                    <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E0E0E0'></horizontal>

                    <horizontal h="40" margin="10 0 10 0">
                        <Switch textSize="14sp" textColor="black" id="auto_up_down"  text="自动上滑、下滑" checked="true" layout_weight="1" h="40"/>
                    </horizontal>
                    <horizontal w="*" h="1sp" margin="0 5 0 5" bg='#E0E0E0'></horizontal>
                    <horizontal w="*" margin="0 40 0 20" gravity="center">
                        <button id="runSrt" text="开始运行" w="240sp" textColor="#FFFFFF" gravity="center" textSize="15sp" />
                    </horizontal>
                </vertical>
                <vertical id="error_404" visibility="gone">
                    <vertical padding="25">
                        <linear orientation="horizontal" gravity="left" marginTop="10">
                            <img src="@drawable/ic_warning_black_48dp" tint="red" h="30"/>
                            <text id="Statuscode" text="404" textStyle="bold" textSize="22" textColor="red"/>
                        </linear>
                        <text id="tip" text="请求错误！" textStyle="bold" textSize="18" margin="0 5 0 5" textColor="#000000"/>
                        <text id="tips" text="这可能是一个严重的服务器端的错误，请先检查您的网络配置是否正确，若多次出现此错误请联系开发者。" textSize="14" margin="0 0 30 0" textColor="#000000"/>
                    </vertical>
                </vertical>
            </vertical>
        </frame>
    </vertical>
);
ui.appbar.backgroundDrawable = GradientDrawable("LEFT_RIGHT", ["#FF0081FF", "#FF1CBBB4"]);
ui.runSrt.backgroundDrawable = GradientDrawable("LEFT_RIGHT", ["#FF0081FF", "#FF1CBBB4"]);
/**
 * 转换成系统支持的颜色
 * @param {颜色} color 
 */
function Color(color) {
    return android.graphics.Color.parseColor(color);
}

/**
 * 配置渐变色
 * @param {方向} orientation 
 * @param {颜色数组} color 
 */
function GradientDrawable(orientation, color) {
    var colors = [];
    color.forEach(color => colors.push(Color(color)));
    return new android.graphics.drawable.GradientDrawable(android.graphics.drawable.GradientDrawable.Orientation[orientation], colors);
}

ui.back.on("click",function(){
    ui.finish();
});

threads.start(initDatas);
var storage = storages.create("game");

function initDatas(){
  
}


/**
 * 格式化时间
 */
function getTime() {
    var fmt = "YYYY-MM-dd hh:mm:ss";
    var d = new Date();
    var hh = d.getHours();
    var mm = d.getMinutes();
    if (mm < 30) {
        mm = 30
    } else {
        hh += 1;
        mm = 0
    }
    var o = {
        "Y+": d.getYear() + 1900,
        "M+": d.getMonth() + 1,
        "d+": d.getDate(),
        "h+": hh,
        "m+": mm,
        "s+": 0
    };
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 4) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    fmt = fmt.replace(/-/g, '/');
    return fmt;
}

// 获取时分秒用于记录日志
function logCommon(msg) {
    console.log(formatDate(getNow()) + " " + msg);
}
function logInfo(msg) {
    console.info(formatDate(getNow()) + " " + msg);
}
function logWarn(msg) {
    console.warn(formatDate(getNow()) + " " + msg);
}
function logError(msg) {
    console.error(formatDate(getNow()) + " " + msg);
}

// 时间戳转时间字符串
function add0(m) {
    return m < 10 ? '0' + m : m
}
// 根据时间偏移值计算真实时间
function getNow() {
    var now = new Date().getTime();
    return now;
}
function add00(m) {
    if (m < 10) {
        return '00' + m;
    } else if (m < 100) {
        return '0' + m;
    } else {
        return m;
    }
}

function formatDate(needTime) {
    //needTime是整数，否则要parseInt转换
    var time = new Date(parseInt(needTime));
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return add0(h) + ':' + add0(mm) + ':' + add0(s);
}

setTimeout(initConfig, 500);

function initConfig() {
    startWaitTime = storage.get("startWaitTime", "8");
    oneAppRunTime = storage.get("oneAppRunTime", "30");
    everydayNumber = storage.get("everydayNumber","4");

    ui.startWaitTime.setText(startWaitTime);
    ui.oneAppRunTime.setText(oneAppRunTime);
    ui.everydayNumber.setText(everydayNumber);

    //随机点赞、随机刷新
    ui.random_up.checked = storage.get("random_up", true);
    ui.auto_refresh.checked = storage.get("auto_refresh", true);
    ui.auto_up_down.checked = storage.get("auto_up_down", true);
}

